<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace common\behavior;

use support\bootstrap\Redis;
use think\module\Module;

/**
 * 行为扩展：处理event
 */
class RecordEventBehavior
{

    /**
     * 处理记录event log
     * @param $params
     */
    public function run(&$params)
    {
        if (empty(request()->getBatchNumber())) {
            request()->getBatchNumber('un_authorized');
        }
        $params['module_data'] = Module::$moduleDictData['module_index_by_code'][$params['param']['table']];
        $data = [
            'level' => (string)$params['level'],
            'operate' => (string)$params['operate'],
            "record" => $params,
            'module_code' => (string)$params['module_data']['code'],
            'link_id' => (int)$params['primary_id'],
            "created_by" => (string)request()->getUserUUID(),
            'batch_number' => (string)request()->getBatchNumber(),
            'belong_system' => (string)C('BELONG_SYSTEM')
        ];

        // 暂存event_log
        Redis::rPush(request()->getBatchNumber(), json_encode($data));
        // 设置event_log得最大存活时间
        Redis::expire(request()->getBatchNumber(), 60 * 60 * 24);
    }
}
